/** 系统基础业务处理 */

function onOpenAddDialog() {
    $("#form").form("clear");
    $("#id").val(0);
    $("#pid").val(0);
    $("#dialog-save").linkbutton({"text": "保存"});
    $("#dialog").dialog("open");
}

function onOpenEditDialog() {
    var rows = $("#table").datagrid("getSelections");
    if (rows == null || rows.length == 0) {
        AlertUtil.warning("请选择后，再操作！");
        return;
    }
    if (rows.length != 1) {
        AlertUtil.warning("每次只能操作一条数据！");
        return;
    }
    var row = rows[0];
    $("#form").form("load", row);
    $("#dialog-save").linkbutton({"text": "提交"});
    $("#dialog").dialog("open");
}

function onSave(url, type) {
    var type = type || "grid";
    var url = url || "save";
    $("#form").ajaxForm({
        "url": url,
        "method": "post",
        "dataType": "json",
        "beforeSubmit": function () {
            return $("#form").form("validate");
        },
        "success": function (data) {
            onResult(data, type);
            $("#dialog").dialog("close");
        }

    }).submit();
}

function onResult(data, type) {
    var type = type || "grid";
    if (data) {
        AlertUtil.info(data.msg);
        if (data.code == 1) {
            onRefresh(type);
        }
    } else {
        AlertUtil.error("处理出错啦，请稍后再试！")
    }
}

function onRemove(type) {
    var type = type || "grid";
    var rows = $("#table").datagrid("getSelections");
    if (rows == null || rows.length == 0) {
        AlertUtil.warning("请选择后，再操作！");
        return;
    }
    $.messager.confirm("提示", "确定删除选择项?", function (isOk) {
        if (isOk) {
            var ids = Common.getIds(rows);
            onRemoveHandler(ids, type);
        }
    });
}

function onRemoveHandler(ids, type) {
    if (ids == null || ids == "") {
        return;
    }
    var type = type || "grid";
    AjaxUtil.post("del/" + ids, "", function (data) {
        onResult(data, type);
    });
}

function onRefresh(type) {
    var type = type || "grid";
    if (type == "grid") {
        onGridRefresh();
    } else {
        onTreeRefresh();
    }
}

function onGridRefresh() {
    $("#table").datagrid("clearSelections");
    $("#table").datagrid("reload");
}

function onTreeRefresh() {
    $("#table").treegrid("unselectAll");
    $("#table").treegrid("reload");
}

function onOpenQueryDialog(){
    $("#dialog-query").dialog("open");
}

function onQuery (){

}

function onResetQuery() {
    $("#form-query").form("clear")
}